using UnityEngine;
using System.Collections;

/// \class  gkSpawningBatchInLineX
/// \brief  Place on line each batch of spawned actors
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawningBatchInLineX")]
public class gkSpawningBatchInLineX : gkSpawningModifier
{
	/// Color unselected
    public Color EditorLineSelectedColor = new Color(0.0f, 0.0f, 0.75f, 0.5f);
	
	/// Color unselected
    public Color EditorLineUnSelectedColor = new Color(0.0f, 0.75f, 0.0f, 0.5f);
	
	/// \brief  Called each frame
    void OnDrawGizmos()
    {
		DebugDraw(EditorLineUnSelectedColor);
    }
	
	/// \brief  Called each frame
    void OnDrawGizmosSelected()
    {
		DebugDraw(EditorLineSelectedColor);
    }
	
    /// \brief  Event called at the and of the actor spawn
    public override void ModifySpawnedActor(GameObject a_rSpawnedActor, int a_iNumActorsInCurrentBatch, int a_iNbActorsInCurrentBatch)
    {
	    PlaceBatchedSpawnedActorInLine(a_rSpawnedActor, a_iNumActorsInCurrentBatch, a_iNbActorsInCurrentBatch);
    }
	
	/// \brief  Debug draw
	private void DebugDraw(Color a_oColor)
	{
		Vector3 f3LineExtent = Vector3.zero;
		
        // In editor always draw the bounding box
        if (Application.isEditor)
        {
			// Compute the line half size
			f3LineExtent.x = collider.bounds.extents.x;
			
            Gizmos.color = a_oColor;
			
            Gizmos.DrawLine(transform.position - f3LineExtent, transform.position + f3LineExtent);
        }
	}

    /// \brief  Place the spawned actor just out of the screen
    void PlaceBatchedSpawnedActorInLine(GameObject a_rSpawnedActor, int a_iNumActorsInCurrentBatch, int a_iNbActorsInCurrentBatch)
    {
	    Vector3 f3SpawnedPosition;
		float fLineHalfSize;
		float fLinePositionPercent;
		
		// Compute the line half size
		fLineHalfSize = collider.bounds.extents.x;
		
		// Compute the spawned actor position in line percent
		fLinePositionPercent = (float)(a_iNumActorsInCurrentBatch + 1)/(float)(a_iNbActorsInCurrentBatch+1);
		
		// Compute the spawned position
	    f3SpawnedPosition = a_rSpawnedActor.transform.position;
		
		f3SpawnedPosition.x = transform.position.x + Mathf.Lerp(-fLineHalfSize, fLineHalfSize, fLinePositionPercent);

	    a_rSpawnedActor.transform.position = f3SpawnedPosition;
    }
}
